package com.googlecode.maurersmarbles.drawables;

import java.util.Map;
import java.util.TreeMap;

import com.googlecode.maurersmarbles.Board;
import com.googlecode.maurersmarbles.GameLogic;
import com.googlecode.maurersmarbles.R;
import com.googlecode.maurersmarbles.Util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class ImageBoardDrawable extends BoardDrawable {

	/**
	 * The bitmap that contains the board background.
	 */
	private static Bitmap BACKGROUND_BITMAP;
	/**
	 * The bitmap that contains the empty space.
	 */
	private static Bitmap EMPTY_SPACE_BITMAP;
	/**
	 * The map that contains the bitmap for each safe space color.
	 */
	private static final Map<Integer, Bitmap> SAFE_SPACE_BITMAP_MAP = new TreeMap<Integer, Bitmap>();
	/**
	 * The paint that is used to draw the discard rectangle.
	 */
	private static Paint DISCARD_PAINT;

	/**
	 * Creates a new ImageBoardDrawable.
	 * @param board the board to draw
	 * @param playerColors the array that contains the player colors
	 */
	public ImageBoardDrawable(Board board, int[] playerColors) {
		super(board, playerColors);
	}

	/**
	 * Sets the context and loads the bitmap images.
	 * @param context the context
	 */
	public static void setContext(Context context) {

		Resources resources = context.getResources();

		BACKGROUND_BITMAP = BitmapFactory.decodeResource(resources,
				R.drawable.classic_board_background);

		SAFE_SPACE_BITMAP_MAP.clear();
		SAFE_SPACE_BITMAP_MAP.put(GameLogic.COLORS[0], BitmapFactory
				.decodeResource(resources,
						R.drawable.classic_board_safe_space_0));
		SAFE_SPACE_BITMAP_MAP.put(GameLogic.COLORS[1], BitmapFactory
				.decodeResource(resources,
						R.drawable.classic_board_safe_space_1));
		SAFE_SPACE_BITMAP_MAP.put(GameLogic.COLORS[2], BitmapFactory
				.decodeResource(resources,
						R.drawable.classic_board_safe_space_2));
		SAFE_SPACE_BITMAP_MAP.put(GameLogic.COLORS[3], BitmapFactory
				.decodeResource(resources,
						R.drawable.classic_board_safe_space_3));

		EMPTY_SPACE_BITMAP = BitmapFactory.decodeResource(resources,
				R.drawable.classic_board_space);

	}

	@Override
	protected void drawBackground(Canvas canvas, int startX, int startY,
			int width, int height) {
		Util.drawImage(canvas, BACKGROUND_BITMAP, startX, startY, width, height);
	}

	@Override
	protected void drawDiscardRectangle(Canvas canvas, int startX, int startY,
			int width, int height) {
		Util.drawEmptyRectangle(canvas, startX, startY, width, height,
				DISCARD_PAINT);
	}

	@Override
	protected void drawEmptySpace(Canvas canvas, int startX, int startY,
			int width, int height) {
		Util.drawImage(canvas, EMPTY_SPACE_BITMAP, startX, startY, width,
				height);
	}

	@Override
	protected void drawHomeSpace(Canvas canvas, int startX, int startY,
			int width, int height, int color) {
		drawSafeSpace(canvas, startX, startY, width, height, color);
	}

	@Override
	protected void drawSafeSpace(Canvas canvas, int startX, int startY,
			int width, int height, int color) {
		Util.drawImage(canvas, SAFE_SPACE_BITMAP_MAP.get(color), startX,
				startY, width, height);
	}

	@Override
	protected void drawStartSpace(Canvas canvas, int startX, int startY,
			int width, int height, int color) {
		drawSafeSpace(canvas, startX, startY, width, height, color);
	}

	static {

		DISCARD_PAINT = new Paint();
		DISCARD_PAINT.setStrokeWidth(2);

	}

}
